package com.itheima.controller;

import com.itheima.anno.LogOperate;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     *查询班级列表
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询全部班级列表");
        List<Clazz> clazzList = clazzService.selectAll();
        return Result.success(clazzList);
    }

    /**
     *新增班级
     * @param clazz
     */
    @PostMapping
    @LogOperate
    public Result save(@RequestBody Clazz clazz){
        log.info("新增班级,clazz: {}", clazz);
        System.out.println(clazz.toString());
        clazzService.insert(clazz);
        return Result.success();
    }

    /**
     * 分页查询班级
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("分页查询，参数：{}", clazzQueryParam);
        // 调用业务分页查询
        PageResult clazzList=clazzService.page(clazzQueryParam);
        return Result.success(clazzList);
    }
    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询, id:{}" + id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }
    /**
     * 删除班级
     */
    @DeleteMapping("/{id}")
    @LogOperate
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除班级，id为：{}", id);
        clazzService.deleteById(id);
        return Result.success();
    }
    /**
     * 修改班级
     */
    @PutMapping
    @LogOperate
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级，clazz:{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }
}
